<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside width="210px" class="sidebar-container">
      <div class="logo-container">
        <div class="logo">高校宿舍管理系统</div>
      </div>
      
      <el-menu
        :default-active="activeIndex"
        class="sidebar-menu"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        router
      >
        <el-menu-item index="/home">
          <el-icon><HomeFilled /></el-icon>
          <span>首页</span>
        </el-menu-item>

        <!-- 宿管菜单 -->
        <template v-if="role === '宿管'">
 
          <!-- fyx -->
          <el-menu-item index="/repair-manager">
            <el-icon><Tools /></el-icon>
            <span>报修管理</span>
          </el-menu-item>
          
          <!-- 许泽坤的 -->
          <el-menu-item index="/dorm-inspection">
            <el-icon><Check /></el-icon>
            <span>内务检查</span>
          </el-menu-item>

          <el-menu-item index="/announcement">
            <el-icon><Document /></el-icon>
            <span>公告通知</span>
          </el-menu-item>
        </template>
        
        <!-- 学生菜单 -->
        <template v-if="role === '学生'">
          <el-menu-item index="/student-repair">
            <el-icon><Tools /></el-icon>
            <span>宿舍报修</span>
          </el-menu-item>
                <!-- fyx把：repair-report改成：my-repairs ---但是没有页面
                <el-menu-item index="/my-repairs">
                  <el-icon><List /></el-icon>
                  <span>我的报修</span>
                </el-menu-item> -->
          <!-- xzk -->
          <el-menu-item index="/student-inspection">
            <el-icon><Check /></el-icon>
            <span>内务检查记录</span>
          </el-menu-item>  
          <el-menu-item index="/announcement">
            <el-icon><Document /></el-icon>
            <span>公告通知</span>
          </el-menu-item>
        </template>
        
        <el-menu-item index="/center">
          <el-icon><User /></el-icon>
          <span>个人中心</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container class="main-container">
      <!-- 头部导航 -->
      <el-header height="60px" class="header-container">
        <div class="header-left">
          <el-icon class="fold-btn" @click="toggleSidebar">
            <Expand v-if="isCollapse" />
            <Fold v-else />
          </el-icon>
          <Breadcrumb />
        </div>
        <div class="header-right">
          <el-dropdown trigger="click">
            <span class="user-info">
              {{ username }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToCenter">个人中心</el-dropdown-item>
                <el-dropdown-item @click="goToChangePassword">修改密码</el-dropdown-item>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 主要内容 -->
      <el-main class="content-container">
        <router-view></router-view>
      </el-main>
      
      <!-- 底部 -->
      <el-footer height="40px" class="footer-container">
        <div class="footer-text">© 2025 高校宿舍管理系统</div>
      </el-footer>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, onMounted, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  HomeFilled, User, UserFilled, Setting, 
  House, Avatar, Tools, Bell, List, 
  Expand, Fold, ArrowDown, Document, Check
} from '@element-plus/icons-vue'
import Breadcrumb from '@/components/common/Breadcrumb.vue'

const router = useRouter()
const route = useRoute()

const isCollapse = ref(false)
const activeIndex = ref('/home')

const role = computed(() => {
  return localStorage.getItem('role') || ''
})

const username = computed(() => {
  return localStorage.getItem('adminName') || '用户'
})

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

// 跳转到个人中心
const goToCenter = () => {
  router.push('/center')
}

// 跳转到修改密码页面
const goToChangePassword = () => {
  router.push('/update-password')
}

// 退出登录
const logout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    localStorage.removeItem('Token')
    localStorage.removeItem('role')
    localStorage.removeItem('sessionTable')
    localStorage.removeItem('adminName')
    router.replace('/login')
    ElMessage({
      type: 'success',
      message: '退出登录成功'
    })
  }).catch(() => {})
}

onMounted(() => {
  activeIndex.value = route.path
})

// 监听路由变化以更新activeIndex
watch(() => route.path, (newPath) => {
  activeIndex.value = newPath
})
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;
}

.sidebar-container {
  background-color: #304156;
  transition: width 0.3s;
  overflow-y: auto;
  overflow-x: hidden;
  
  .logo-container {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #2b3649;
    
    .logo {
      color: #fff;
      font-size: 18px;
      font-weight: bold;
    }
  }
  
  .sidebar-menu {
    border-right: none;
  }
}

.main-container {
  overflow: hidden;
  
  .header-container {
    background-color: #fff;
    border-bottom: 1px solid #e6e6e6;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    
    .header-left {
      display: flex;
      align-items: center;
      
      .fold-btn {
        font-size: 20px;
        cursor: pointer;
        margin-right: 15px;
      }
    }
    
    .header-right {
      .user-info {
        cursor: pointer;
        display: flex;
        align-items: center;
        font-size: 14px;
        
        .el-icon {
          margin-left: 5px;
        }
      }
    }
  }
  
  .content-container {
    padding: 20px;
    overflow-y: auto;
  }
  
  .footer-container {
    background-color: #fff;
    border-top: 1px solid #e6e6e6;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .footer-text {
      color: #666;
      font-size: 12px;
    }
  }
}

:deep(.el-menu) {
  border-right: none;
}
</style> 